// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_MODE, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import ToastManager from "../manager/ToastManager";
import DataManager from "../manager/DataManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ReadyLayer extends BaseLayer {

    nodePanel: cc.Node = null
    btnClose: cc.Node = null
    btnStart: cc.Node = null

    mainNode: cc.Node = null
    isCanPlay: boolean = false
    choosed: number[] = [0, 0, 1, 1, 1]

    onLoad() {
        this.nodePanel = cc.find('style/panel', this.node)
        this.btnClose = cc.find('btn_close', this.nodePanel)
        this.btnStart = cc.find('btn_start', this.nodePanel)
        this.btnClose.on('click', this.onCloseClick, this)
        this.btnStart.on('click', this.onStartClick, this)
        this.mainNode = cc.find('main', this.nodePanel)
    }

    onDestroy() {
        this.btnClose.off('click', this.onCloseClick, this)
        this.btnStart.off('click', this.onStartClick, this)
    }

    onEnable() {
        this.zoomIn(this.nodePanel)
        this.isCanPlay = false
        this.choosed = [0, 0, 1, 1, 1]
        this.rendor()
    }

    onDisable() { }

    onCloseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.READY, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
    }

    onStartClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (this.isCanPlay) {
            DataManager.instance.mode = ENUM_GAME_MODE.SINGLE
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.READY, false)
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN)
            StaticInstance.gameManager.onGameStart()
        } else {
            ToastManager.instance.show('至少配置两个阵营', { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
        }
    }

    rendor(isUpdate: number = -1) {
        if (isUpdate >= 0) {
            const buttons = this.mainNode.children[isUpdate].getChildByName('buttons')
            buttons.children.forEach((button: cc.Node, key: number) => {
                const btnOn = button.getChildByName('on')
                const btnOff = button.getChildByName('off')
                btnOn.active = key == this.choosed[isUpdate]
                btnOff.active = !(key == this.choosed[isUpdate])
            })
        } else {
            this.mainNode.children.forEach((item: cc.Node, index: number) => {
                const buttons = item.getChildByName('buttons')
                buttons.children.forEach((button: cc.Node, key: number) => {
                    const btnOn = button.getChildByName('on')
                    const btnOff = button.getChildByName('off')
                    btnOn.active = key == this.choosed[index]
                    btnOff.active = !(key == this.choosed[index])
                    if (!button.hasEventListener('click')) {
                        button.on('click', () => { this.onButtonCLick(index, key) })
                    }
                })
            })
        }
        // 修改游戏数据
        for (let i = 0; i < this.choosed.length; i++) {
            if (i == 0) {
                DataManager.instance.canFlyIndex = this.choosed[0]
            } else {
                DataManager.instance.posData[i - 1] = [-2, -2, -2, -2]
                // DataManager.instance.posData[i - 1] = [12, 52, 3, 44]
                // DataManager.instance.posData[i - 1] = [55, 55, 55, 52]
                if (this.choosed[i] == 2) {
                    DataManager.instance.posData[i - 1] = []
                } else {
                    DataManager.instance.isAi[i - 1] = this.choosed[i] == 1
                }
            }
        }
        // 判断数据合法性（存在两个玩家）
        let count = 0
        for (let i = 0; i < DataManager.instance.posData.length; i++) {
            if (DataManager.instance.posData[i].length > 0) {
                count += 1
            }
        }
        // 状态
        if (count < 2) {
            this.isCanPlay = false
        } else {
            this.isCanPlay = true
            // 谁是第一回合
            for (let i = 0; i < DataManager.instance.posData.length; i++) {
                if (DataManager.instance.posData[i].length > 0) {
                    DataManager.instance.round = i
                    break
                }
            }
        }
    }

    onButtonCLick(key: number, val: number) {
        this.choosed[key] = val
        this.rendor(key)
    }
}
